package design.simdi.code.final_paper.site.panels.administrare.absolventi;

import java.util.UUID;

import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.wizard.WizardModel;
import org.apache.wicket.extensions.wizard.WizardStep;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.value.ValueMap;
import org.apache.wicket.validation.validator.EmailAddressValidator;

import design.simdi.code.final_paper.core.entities.Absolvent;
import design.simdi.code.final_paper.core.entities.Person;
import design.simdi.code.final_paper.core.entities.User;
import design.simdi.code.final_paper.core.services.ServiceException;
import design.simdi.code.final_paper.core.services.UserService;
import design.simdi.code.final_paper.site.application.WicketApplication;
import design.simdi.code.final_paper.site.pages.BasePage;
import design.simdi.code.final_paper.site.panels.MyWizard;
import design.simdi.code.final_paper.site.panels.MyWizardButtonBar;

/**
 * 
 * @author Marius Dumitriu - (Jun 25, 2010 - 7:00:57 PM)
 * 
 */
public class AddAbsolventWizardPanel extends MyWizard {

	private static final long serialVersionUID = 1L;

	private Person person;

	private PasswordTextField password;
	private TextField<String> username;
	private TextField<String> email;

	private TextField<String> firstName;
	private TextField<String> lastName;

	private TextField<Integer> an;

	private String title;
	private String subtitle;
	private final ValueMap properties = new ValueMap();

	@SpringBean
	private UserService userService;

	public AddAbsolventWizardPanel(String id, Person person) {
		super(id, false);

		if (person == null) {
			person = new Person();
			person.setAbsolvent(new Absolvent());
			person.setUser(new User());
			title = "form.add.title";
			subtitle = "form.add.subtitle";
		} else {
			title = "form.modify.title";
			subtitle = "form.modify.subtitle";

		}
		this.person = person;

		setDefaultModel(new CompoundPropertyModel<AddAbsolventWizardPanel>(this));
		WizardModel model = new WizardModel();
		model.add(new PersonAbsolventStep());
		model.add(new PersonUserStep());

		init(model);
		((WicketApplication) getApplication()).setShowErrors(false);
	}

	protected Component newButtonBar(String id) {
		return new MyWizardButtonBar(id, this, getForm()) {

			private static final long serialVersionUID = 1L;

			public void onFinishClick(AjaxRequestTarget target) {
				try {
					onFinish(target);
					AbsolventiTabPanel panel = new AbsolventiTabPanel("panel");
					panel.setOutputMarkupId(true);

					Page page = this.getPage();

					page.get("content:tabs:panel").replaceWith(panel);

					target.addComponent(page.get("content"));

				} catch (ServiceException e) {

					error(e.getMessage());
				}

			}

			public void onCancelClick(AjaxRequestTarget target) {
				onCancel(target);
				AbsolventiTabPanel panel = new AbsolventiTabPanel("panel");
				panel.setOutputMarkupId(true);

				Page page = this.getPage();

				page.get("content:tabs:panel").replaceWith(panel);

				target.addComponent(page.get("content"));
				target.addComponent(page.get(BasePage.ERRORS_PANEL));
			}

			public void onNextClick(AjaxRequestTarget target) {

				Page page = this.getPage();
				target.addComponent(page.get(BasePage.ERRORS_PANEL));
			}
		};
	}

	public void onFinish(AjaxRequestTarget target) throws ServiceException {
		if (person.getUser().getId() == null)
			person.getUser().setPassword(password.getModelObject());
		userService.addAbsolventUser(person);
		onFinishMessage(target);
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserService getUserService() {
		return userService;
	}

	private final class PersonAbsolventStep extends WizardStep {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public PersonAbsolventStep() {
			super(new ResourceModel(title), new ResourceModel(subtitle));

			// detalii persoana
			add(firstName = new TextField<String>("firstName", new PropertyModel<String>(person, "prenume")));
			add(lastName = new TextField<String>("lastName", new PropertyModel<String>(person, "nume")));
			// nu mai pun CNP la profesor

			// detali student
			add(an = new TextField<Integer>("an", new PropertyModel<Integer>(person.getAbsolvent(), "anAbsolvire")));
			an.setRequired(false);
			firstName.setRequired(true);
			lastName.setRequired(true);

			firstName.setType(String.class);
			lastName.setType(String.class);
			an.setType(Integer.class);

		}
	}

	private final class PersonUserStep extends WizardStep {

		private static final long serialVersionUID = 1L;

		public PersonUserStep() {
			setTitleModel(new ResourceModel("userdetails.title"));
			setSummaryModel(new StringResourceModel("userdetails.summary", this, new Model<User>(person.getUser())));
			WebMarkupContainer labelparola;

			add(labelparola = new WebMarkupContainer("password.label"));

			add(username = new TextField<String>("username", new PropertyModel<String>(person.getUser(), "login")));
			add(password = new PasswordTextField("password", new PropertyModel<String>(properties, "password")));
			add(email = new TextField<String>("email", new PropertyModel<String>(person.getUser(), "email")));

			AjaxButton butonGenerate;

			add(butonGenerate = new AjaxButton("pass-generator") {

				private static final long serialVersionUID = 1L;

				@Override
				protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
					password.setDefaultModel(new Model<String>("absolvent"));
					// cand se genereaza parole .. trebuie scos comentariu la
					// randu de mai jos
					 password.setDefaultModelObject(UUID.randomUUID().toString());
					target.addComponent(password);

				}
			});
			password.setOutputMarkupPlaceholderTag(true);
			password.setResetPassword(false);
			labelparola.setOutputMarkupPlaceholderTag(true);
			butonGenerate.setOutputMarkupPlaceholderTag(true);

			butonGenerate.setDefaultFormProcessing(false);

			password.setVisible(person.getUser().getId() == null);
			labelparola.setVisible(person.getUser().getId() == null);
			butonGenerate.setVisible(person.getUser().getId() == null);

			username.setRequired(true);
			email.setRequired(true);
			password.setRequired(true);

			username.setType(String.class);
			password.setType(String.class);
			email.setType(String.class);

			email.add(EmailAddressValidator.getInstance());

		}
	}
}
